﻿using System;
using System.ComponentModel;

using System.Text;

namespace MT.eVotePRS.Model
{
    public abstract class BaseModel 
        : IModel, INotifyPropertyChanged
    {
        public const string FLAG_INSERT = "I";
        public const string FLAG_UPDATE = "U";
        public const string FLAG_DELETE = "D";

        public BaseModel()
        {

        }

        private long? id;
        public long? Id { get { return id; } set { if (id != value) { id = value; OnPropertyChanged("Id"); } } }


        private string recDelInd;
        public string RecDelInd { get { return recDelInd; } set { if (recDelInd != value) { recDelInd = value; OnPropertyChanged("RecDelInd"); } } }

        private DateTime? lastRecTxnDate;
        public DateTime? LastRecTxnDate { get { return lastRecTxnDate; } set { if (lastRecTxnDate != value) { lastRecTxnDate = value; OnPropertyChanged("LastRecTxnDate"); } } }

        public bool HasIdentity { get { return id != null && id != 0; } }
        public bool IsDeleted { get { return this.isLoadFromDb && editFlag.IndexOf(FLAG_DELETE) >= 0;} }
        public bool IsUpdated { get { return this.isLoadFromDb && editFlag.IndexOf(FLAG_UPDATE) >= 0; } }
        public bool IsNew { get { return !this.isLoadFromDb && editFlag.IndexOf(FLAG_INSERT ) >= 0 ;} }

        /// <summary>
        /// construct from iBatis
        /// </summary>
        private bool isInit;
        private bool isLoadFromDb;
        public bool DBBeginInit { set { isInit = true; isLoadFromDb = true; } }
        public bool DBEndInit { set { isInit = false; } }


        private string editFlag;

        #region INotifyPropertyChanged Members
        
        public event PropertyChangedEventHandler PropertyChanged;
        
        #endregion


        protected void OnPropertyChanged(string propName)
        {
            if (!isInit)
            {
                MarkUpdate();
            }

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        protected void OnNoMarkUpdatePropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public void MarkInsert()
        {
            isLoadFromDb = false;
            editFlag = FLAG_INSERT;
        }

        public void MarkUpdate()
        {
            if (editFlag.IndexOf(FLAG_UPDATE) > 0) return;
            editFlag = editFlag + FLAG_UPDATE;
        }

        public void MarkDelete()
        {
            if (editFlag.IndexOf(FLAG_DELETE) > 0) return;
            editFlag = editFlag + FLAG_DELETE;
        }

        public void MarkClean()
        {
            editFlag = string.Empty;
        }        

        //public override string ToString()
        //{
            // return ReflectionToStringBuilder.ReflectionToString(this);            
        //}
    }
}
